package com.taoyibao.hongcanting.ui.login.delegate;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.delegate.ToolbarDelegate;
import com.taoyibao.hongcanting.constant.CodeConstant;
import com.taoyibao.hongcanting.utils.CodeUtils;
import com.taoyibao.hongcanting.utils.GlideUtil;
import com.taoyibao.hongcanting.utils.ToastUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author TangAnna
 * @description:
 * @date :${DATA} 13:25
 */
public class SetPersonInfoDelegate extends ToolbarDelegate implements TextWatcher, RadioGroup.OnCheckedChangeListener {
    public String mHeadUrl;//头像地址
    private ImageView mImageView;
    private TextView mTvBirthday;
    private Button mBtnConfirm;
    private EditText mEtNickName;
    private RadioGroup radioGroup;

    @Override
    public int getRootLayoutId() {
        return R.layout.activity_set_personinfo;
    }

    @Override
    public void initWidget() {
        super.initWidget();
        setTitle("设置个人资料");
        mImageView = get(R.id.iv_setInfo_touxiang);
        mBtnConfirm = get(R.id.btn_setPersonInfo_confirm);
        mEtNickName = get(R.id.et_setPeronInfo_nickName);
        mEtNickName.addTextChangedListener(this);
        radioGroup = get(R.id.rg_setPersonInfo_xingBie);
        radioGroup.setOnCheckedChangeListener(this);
    }

    /**
     * 设置头像
     *
     * @param url
     */
    public void setHeadUrl(String url) {
        mHeadUrl = url;
        GlideUtil.getInstance().loadDefaultCircleHead(getActivity(), url, mImageView);
        setConfirmEnable();
    }

    /**
     * 判断底部按钮是否可点击
     */
    public void setConfirmEnable() {
        if (!TextUtils.isEmpty(mHeadUrl) && !TextUtils.isEmpty(getBirthday()) && !TextUtils.isEmpty(getNickName()) && !TextUtils.isEmpty(getGrnder())) {
            mBtnConfirm.setEnabled(true);
            mBtnConfirm.setClickable(true);
        } else {
            mBtnConfirm.setEnabled(false);
            mBtnConfirm.setClickable(false);
        }
    }

    /**
     * 获取选择的生日
     *
     * @return
     */
    public String getBirthday() {
        return ((TextView) get(R.id.et_setPeronInfo_birthday)).getText().toString().trim();
    }

    /**
     * 设置生日
     *
     * @param date
     */
    public void setBirthday(String date) {
        if (!TextUtils.isEmpty(date)) {
            ((TextView) get(R.id.et_setPeronInfo_birthday)).setText(date);
        }
        setConfirmEnable();
    }


    /**
     * 获取昵称
     *
     * @return
     */
    public String getNickName() {
        return mEtNickName.getText().toString().trim();
    }

    /**
     * 获取性别
     *
     * @return
     */
    public String getGrnder() {
        if (radioGroup.getCheckedRadioButtonId() == R.id.ra_setPersonInfo_nan) {
            return CodeConstant.MALE + "";
        } else if (radioGroup.getCheckedRadioButtonId() == R.id.ra_setPersonInfo_nv) {
            return CodeConstant.FEMALE + "";
        }
        return "";
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        // 过滤只能以字母或汉字开头
        String input = mEtNickName.getText().toString().trim();
        if (!TextUtils.isEmpty(input)) {
            String nickMatcher = "^[\\u4e00-\\u9fa5a-zA-Z]+$";//正则匹配只能以英文或者汉字开头
            Pattern p = Pattern.compile(nickMatcher);
            Matcher m = p.matcher(input.charAt(0) + "");
            if (!m.matches()) {
                mEtNickName.setText("");
                ToastUtils.showToast(CodeUtils.getIdString(R.string.persion_nick_input));
            }
        }
        setConfirmEnable();
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        setConfirmEnable();
    }
}
